package com.sjz.han.industry.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjz.han.industry.R;
import com.sjz.han.industry.activites.MemberDetailActivity;
import com.sjz.han.industry.entities.MainTeamEntity;

import net.cachapa.expandablelayout.ExpandableLinearLayout;

import java.util.ArrayList;
import java.util.List;


/**
 *团队适配器
 */
public class MainTeam02Adapter extends RecyclerView.Adapter<MainTeam02Adapter.ViewHolder> {
    private Context context;
    public List<MainTeamEntity.MainTeamData.MainUser.C2User.C1User> datas = new ArrayList<>();
    public MainTeam02Adapter(Context context) {
        this.context=context;
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
//        return 3;
    }
    public void setAdaterDatas(List<MainTeamEntity.MainTeamData.MainUser.C2User.C1User> datas){
        this.datas=datas;
        notifyDataSetChanged();
    }
    @Override
    public MainTeam02Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainTeam02Adapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_team2_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final MainTeam02Adapter.ViewHolder holder, final int position) {
        MainTeam03Adapter teamDownAdapter=new MainTeam03Adapter(context);
        holder.team_two_recy.setLayoutManager(new LinearLayoutManager(context));
        holder.team_two_recy.setAdapter(teamDownAdapter);

        if(datas.get(position).nextList!=null){
            teamDownAdapter.setAdapterDatas(datas.get(position).nextList);
            if("2".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("普通会员("+datas.get(position).nextList.size()+")");
            }else if("3".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("A级会员("+datas.get(position).nextList.size()+")");
            }else if("4".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("B级会员("+datas.get(position).nextList.size()+")");
            }else if("5".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("一级会员("+datas.get(position).nextList.size()+")");
            }else if("6".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("二级会员("+datas.get(position).nextList.size()+")");
            }
        }else{
            if("2".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("普通会员(0)");
            }else if("3".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("A级会员(0)");
            }else if("4".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("B级会员(0)");
            }else if("5".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("一级会员(0)");
            }else if("6".equals(datas.get(position).level)){
                holder.teamdown_title_tv.setText("二级会员(0)");
            }
        }
        if(datas.get(position).nextList!=null){
            holder.team02_updown_rel.setEnabled(true);
        }else{
            holder.team02_updown_rel.setEnabled(false);
        }
        holder.team02_updown_rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(holder.team_expand.isExpanded()){
                    holder.team_expand.collapse();
                    holder.team_updown_img.setImageResource(R.mipmap.team_green_jt);
                }else{
                    holder.team_expand.expand();
                    holder.team_updown_img.setImageResource(R.mipmap.team_greenjt_icon);
                }
            }
        });
        holder.team02_photo_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, MemberDetailActivity.class).putExtra("userId",datas.get(position).userId));
            }
        });

        holder.team_name_tv.setText("姓名："+datas.get(position).username);
        holder.team_phone_tv.setText("联系电话："+datas.get(position).phone);
        if(datas.get(position).nextList!=null){
            holder.team_temanum_tv.setText("下属团队："+datas.get(position).nextList.size()+"人");
        }else{
            holder.team_temanum_tv.setText("下属团队：0人");
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public ExpandableLinearLayout team_expand;
        public RelativeLayout team02_updown_rel;
        public ImageView team_updown_img,team02_photo_img;
        public RecyclerView team_two_recy;
        public TextView team_name_tv,team_phone_tv,team_temanum_tv;
        public TextView teamdown_title_tv;

        public ViewHolder(View itemView) {
            super(itemView);
            team_expand=(ExpandableLinearLayout)itemView.findViewById(R.id.team02_expand);
            team02_updown_rel=(RelativeLayout)itemView.findViewById(R.id.team02_updown_rel);
            team_updown_img=(ImageView)itemView.findViewById(R.id.team02_updown_img);
            team_two_recy=(RecyclerView)itemView.findViewById(R.id.team02_two_recy);
            team02_photo_img=(ImageView)itemView.findViewById(R.id.team02_photo_img);

            team_name_tv=(TextView)itemView.findViewById(R.id.team02_name_tv);
            team_phone_tv=(TextView)itemView.findViewById(R.id.team02_phone_tv);
            team_temanum_tv=(TextView)itemView.findViewById(R.id.team02_teamnum_tv);
            teamdown_title_tv=(TextView)itemView.findViewById(R.id.teamdown02_title_tv);

        }

    }
}